package cn.wolfcode.wolf2w.business.service.impl;
import cn.wolfcode.wolf2w.api.RemoteDestinationService;
import cn.wolfcode.wolf2w.api.domain.Destination;
import cn.wolfcode.wolf2w.api.domain.Note;
import cn.wolfcode.wolf2w.api.domain.NoteContent;
import cn.wolfcode.wolf2w.business.mapper.NoteContentMapper;
import cn.wolfcode.wolf2w.business.mapper.NoteMapper;
import cn.wolfcode.wolf2w.business.query.NoteCondition;
import cn.wolfcode.wolf2w.business.query.NoteQuery;
import cn.wolfcode.wolf2w.business.service.INoteService;
import cn.wolfcode.wolf2w.common.core.context.SecurityContextHolder;
import cn.wolfcode.wolf2w.common.redis.service.RedisService;
import cn.wolfcode.wolf2w.common.redis.util.RedisKeys;
import cn.wolfcode.wolf2w.member.api.RemoteUserInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Set;

/**
 * 旅游日记Service业务层处理
 * 
 * @author 浪兎兎
 * @date 2025-05-30
 */
@Service
@Transactional
public class NoteServiceImpl extends ServiceImpl<NoteMapper,Note> implements INoteService {
    
    @Autowired
    private RemoteUserInfoService userInfoService;

    @Autowired
    private NoteContentMapper noteContentMapper;

    @Autowired
    private RemoteDestinationService destinationService;

    @Autowired
    private RedisService redisService;

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Override
    public IPage<Note> queryPage(NoteQuery qo) {
        IPage<Note> page = new Page<>(qo.getCurrentPage(), qo.getPageSize());
        // 条件
        QueryWrapper<Note> wrapper = new QueryWrapper<>();
        // 范围查询条件
        NoteCondition dayMap = NoteCondition.DATE_MAP.get(qo.getDayType());
        if (dayMap != null) {
            wrapper.between("days", dayMap.getMin(), dayMap.getMax());
        }
        NoteCondition consumeMap = NoteCondition.CONSUME_MAP.get(qo.getConsumeType());
        if (consumeMap != null) {
            wrapper.between("avg_consume", consumeMap.getMin(), consumeMap.getMax());
        }
        NoteCondition timeMap = NoteCondition.TIME_MAP.get(qo.getTravelTimeType());
        if (timeMap != null) {
            wrapper.between("MONTH(travel_time)", timeMap.getMin(), timeMap.getMax());
        }
        wrapper.orderByDesc(qo.getOrderBy());
        // 查询所有游记的作者
        IPage<Note> result = baseMapper.selectPage(page, wrapper);
        result.getRecords().forEach(note -> note.setAuthor(userInfoService.getOne(note.getAuthorId(), "inner").getData()));
        return result;
    }

    @Override
    public Note getDetail(Long id) {
        Note note = baseMapper.selectById(id);
        // 查询用户
        note.setAuthor(userInfoService.getOne(note.getAuthorId(), "inner").getData());
        // 查询正文
        NoteContent content = noteContentMapper.selectById(note.getId());
        note.setContent(content);
        return note;
    }

    @Override
    public void add(Note note) {
        // 主表信息
        Destination destination = destinationService.getOne(note.getDestId(), "inner").getData();
        note.setDestName(destination.getName());
        Long userId = SecurityContextHolder.getUserId();
        note.setAuthorId(userId);
        note.setCreateTime(new Date());
        note.setUpdateTime(new Date());
        note.setViewnum(0);
        note.setReplynum(0);
        note.setFavornum(0);
        note.setSharenum(0);
        note.setThumbsupnum(0);
        note.setStatus("0");
        baseMapper.insert(note);

        // 正文信息
        NoteContent content = new NoteContent();
        content.setId(note.getId());
        content.setContent(note.getContent().getContent());
        noteContentMapper.insert(content);

    }

    @Override
    public void reSendNoteMessage() {
        long now = System.currentTimeMillis();
        long max = now - 60*1000;
        String key = RedisKeys.MQ_MESSAGE.join("note");
        Set set = redisService.rangeCacheZsetByScore(key, 0, max);
        for (Object o : set) {
            String message = (String) o;
            amqpTemplate.convertAndSend("noteExchange", "", message);
        }
    }
}
